package com.MusicWeb.servlet;

import com.MusicWeb.entity.Information;
import com.MusicWeb.service.InformationService;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/commentinster")

//todo: 评论插入

//http://localhost:8080/commentinster?name=?&&text=?&&music_id=?&&user_id=?
public class CommentinsterServlet extends HttpServlet {
    InformationService informationService=new InformationService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");
        String mid=req.getParameter("music_id");
        String uid=req.getParameter("user_id");
        String name=req.getParameter("name");
        String text=req.getParameter("text");

        Information information=new Information();
        information.setMusic_id(Integer.parseInt(mid));
        information.setUser_id(Integer.parseInt(uid));
        information.setInformation_text(text);
        information.setName(name);


        if (information.getInformation_text()!=null&&informationService.inster(information)!=null){
            // 设置返回的数据为json对象
            // 把实体转换为json，返回给前端
            String str = new Gson().toJson(information);
            resp.getWriter().print(str);
            req.getSession().setAttribute("msg", "评论成功");
            resp.sendRedirect(req.getContextPath()+"/FunctionPage/MusicReply.jsp?id="+mid);
        }else {
            req.getSession().setAttribute("msg", "网络错误");
        }

    }
}
